package main.controller.admin;

import main.RequestHelper;
import main.TimeHelper;
import main.database.AccountHelper;
import main.database.NotifyHelper;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;

@Controller
public class NotifyPageController {
    @RequestMapping (value = "/notifymanage")
    public String notifyManager(HttpServletRequest request, ModelMap modelMap) {
        String uuid = RequestHelper.getUserUUIDFromRequest(request);
        boolean isAdmin = AccountHelper.isAdminFromUUID(uuid);
        boolean isLogin = AccountHelper.isExistFromUUID(uuid);

        String resultUrl = "admin/notifyPage";
        modelMap.put("isLogin", isLogin);
        if (!isAdmin) {
            resultUrl = "/login";
        }
        else {
            String userName = AccountHelper.getAccountInfoFromUUID(uuid);
            long unread = NotifyHelper.countUnreadNotify(uuid);
            modelMap.put("userName", userName);
            modelMap.put("unread", unread);
            modelMap.put("year2year", TimeHelper.getFormattedYear2Year());
        }

        return resultUrl;
    }
}
